package zw_801_900.zw_836_矩形重叠;

class Solution {

    public static void main(String[] args) {
        int[] rec1 = {0,0,1,1}, rec2 = {1,0,2,1};
        boolean overlap = isRectangleOverlap(rec1, rec2);
        System.out.println(overlap);
    }


    /**
     * 矩形重叠是二维的问题，所以情况很多，比较复杂。为了简化问题，我们可以考虑将二维问题转化为一维问题。
     * 分别投影到x轴和y轴上，如果都重叠了，则重叠。
     * @param rec1
     * @param rec2
     * @return
     */
    public static boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        if (rec1[0] == rec1[2] || rec1[1] == rec1[3] || rec2[0] == rec2[2] || rec2[1] == rec2[3]) return false;
        boolean x_overlap = !(rec1[2] <= rec2[0] || rec2[2] <= rec1[0]);
        boolean y_overlap = !(rec1[3] <= rec2[1] || rec2[3] <= rec1[1]);
        return x_overlap && y_overlap;
//        return !(rec1[2] <= rec2[0] ||   // left
//                rec1[3] <= rec2[1] ||   // bottom
//                rec1[0] >= rec2[2] ||   // right
//                rec1[1] >= rec2[3]);    // top
    }
}